package com.jobsys.utils;

import java.util.HashMap;
import java.util.Map;

import us.codecraft.webmagic.Spider;

/**
 * 线程池
 * 
 * @author pc-12
 *
 */
public class SpiderThreadPool {
	public static Map<String, Spider> pool = new HashMap<String, Spider>();

	/**
	 * 添加
	 * 
	 * @param e
	 */
	public static void add(String key, Spider e) {
		synchronized (pool) {
			if (pool.containsKey(key)) {
				if (pool.get(key) != null) {
					pool.get(key).stop();
				}

				remove(key);
			}

			pool.put(key, e);
		}
	}

	/**
	 * 移除
	 * 
	 * @param e
	 */
	public static synchronized void remove(String key) {
		pool.remove(key);
	}

	/**
	 * 销毁线程池
	 */
	public static void destroy() {
		if (pool == null || pool.size() == 0)
			return;

		for (Spider spider : pool.values()) {
			spider.stop();
		}

		pool.clear();
	}
}
